#include <boost/fiber/all.hpp>
#include <thread>
#include <vector>

void io_task(int id) {
    std::cout << "Task " << id << " start on thread " 
              << std::this_thread::get_id() << std::endl;
    
    // 模拟 I/O 操作
    boost::this_fiber::sleep_for(std::chrono::milliseconds(100));
    
    std::cout << "Task " << id << " yield" << std::endl;
    boost::this_fiber::yield();
    
    boost::this_fiber::sleep_for(std::chrono::milliseconds(100));
    std::cout << "Task " << id << " finish" << std::endl;
}

int main() {
    std::vector<std::thread> threads;
    
    // 创建多个线程，每个线程运行多个 fiber
    for (int i = 0; i < 2; ++i) {
        threads.emplace_back([i]() {
            boost::fibers::fiber f1([i]() { io_task(i * 2 + 1); });
            boost::fibers::fiber f2([i]() { io_task(i * 2 + 2); });
            
            f1.join();
            f2.join();
        });
    }
    
    for (auto& t : threads) {
        t.join();
    }
    
    return 0;
}